package com.ebt.m.msgnote.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.activity.utils.WebBaseActivityUtil;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.msgnote.MsgConstant;
import com.ebt.m.msgnote.TypeMsgDateItemView;
import com.ebt.m.msgnote.TypeMsgItemView;
import com.ebt.m.msgnote.bean.TypeMsgItems;
import com.ebt.m.msgnote.presenter.TypeMsgListPresenter;

import static com.ebt.m.msgnote.MsgConstant.MSG_DATE_VIEW_TYPE;

/**
 * 二级分类消息列表
 *
 * @author allen
 * Created by allen on 2018/7/30 14:35.
 */
public class TypeMsgListView extends BaseListView<TypeMsgListPresenter> {
    public TypeMsgListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TypeMsgListView(Context context) {
        super(context);
    }

    public TypeMsgListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        mRefreshLayout.setRefreshEnable(false);
//        mRefreshLayout.setRefreshNeeded(false);
    }

    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            if (viewType == MSG_DATE_VIEW_TYPE) {
                TypeMsgDateItemView itemView = new TypeMsgDateItemView(getContext());
                return itemView;
            } else {
                TypeMsgItemView itemView = new TypeMsgItemView(getContext());
                itemView.setOnClickViewListener(onClickListner);
                return itemView;
            }
        };
    }


    @Override
    protected TypeMsgListPresenter createPresenter() {
        return new TypeMsgListPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {
        mPresenter.loadNext();
    }

    @Override
    protected boolean createFootView() {
        return true;
    }


    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            switch (view.getId()) {
                case R.id.rlItemViewMsg:
                    TypeMsgItems.TypeMsg bean = (TypeMsgItems.TypeMsg) args[0];
                    if (bean.getJumpMode() == MsgConstant.JUMP_MODE_URL) {
                        String title = bean.getTitle();
                        if (TextUtils.isEmpty(bean.getTitle())) {
                            title = "";
                        } else if (title.equals(getContext().getString(R.string.msg_proposal_alert))) {
                            title = getContext().getString(R.string.guijichakan);
                        }
                        WebBaseActivityUtil.gotoWeb(getContext(), false, title, bean.getUrl(), false, false);
                    } else if (bean.getJumpMode() == MsgConstant.JUMP_MODE_APP) {

                    } else {

                    }
                    if (bean.getStatus() == MsgConstant.MSG_UNREAD) {
                        mPresenter.readMsg(bean.getId());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    @Override
    public void update(Object... args) {
        super.update(args);
        mPresenter.loadNew(args);
    }

    @Override
    protected int setListviewColor() {
        return ContextCompat.getColor(getContext(), R.color.bg_color_F5F5F5);
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.layout_product_search_empty, null);
        ImageView imageView = (ImageView) emptyContent.findViewById(R.id.img_empty);
        TextView textView = (TextView) emptyContent.findViewById(R.id.mession_empty);
        imageView.setBackgroundResource(R.drawable.ic_message_center_empty);
        textView.setText("暂无消息");
        return emptyContent;
    }
}
